#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
View the raw mesh without any processing - closest to Gmsh's native view.
"""

import pyvista as pv
import os
import sys

# Add the dragonfly_simulation directory to Python path
DRAGONFLY_SIM_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(DRAGONFLY_SIM_ROOT)

import config

def view_raw_mesh():
    """Show the mesh exactly as PyVista reads it, without processing."""
    
    mesh_file = os.path.join(DRAGONFLY_SIM_ROOT, config.MESH_FILE)
    
    if not os.path.exists(mesh_file):
        print(f"ERROR: Mesh file not found at {mesh_file}")
        print("Please run 'python generate_mesh.py' first!")
        return
    
    print("Loading mesh...")
    mesh = pv.read(mesh_file)
    print(f"Mesh loaded: {mesh.n_points} points, {mesh.n_cells} cells")
    
    # Create plotter
    plotter = pv.Plotter(window_size=[1600, 900])
    plotter.add_text("Raw Mesh (No Processing)", position='upper_left', font_size=20)
    
    # Show the mesh as-is, without any processing
    print("Showing raw mesh...")
    plotter.add_mesh(mesh, show_edges=True, opacity=0.8, color='lightgray')
    
    # Add coordinate axes
    plotter.add_axes()
    
    # Add information about the mesh
    plotter.add_text(f"Points: {mesh.n_points}\n" +
                    f"Cells: {mesh.n_cells}\n" +
                    f"Cell types: {mesh.celltypes}", 
                    position='lower_left', font_size=12)
    
    # Show the plot
    print("\nShowing raw mesh visualization...")
    print("This should be closer to what you see in Gmsh")
    print("Controls:")
    print("  - Left click + drag: Rotate")
    print("  - Middle click + drag: Pan")
    print("  - Right click + drag / Scroll: Zoom")
    print("  - Q: Quit")
    
    plotter.show()

def compare_views():
    """Show both raw mesh and surface extraction side by side."""
    
    mesh_file = os.path.join(DRAGONFLY_SIM_ROOT, config.MESH_FILE)
    
    if not os.path.exists(mesh_file):
        print(f"ERROR: Mesh file not found at {mesh_file}")
        return
    
    print("Loading mesh for comparison...")
    mesh = pv.read(mesh_file)
    
    # Create side-by-side comparison
    plotter = pv.Plotter(shape=(1, 2), window_size=[1600, 800])
    
    # Left: Raw mesh
    plotter.subplot(0, 0)
    plotter.add_text("Raw Mesh (Like Gmsh)", position='upper_left', font_size=16)
    plotter.add_mesh(mesh, show_edges=True, opacity=0.8, color='lightgray')
    plotter.add_axes()
    
    # Right: Surface extraction (what the original script does)
    plotter.subplot(0, 1)
    plotter.add_text("Surface Extraction (Python Script)", position='upper_left', font_size=16)
    surface = mesh.extract_surface()
    plotter.add_mesh(surface, show_edges=True, opacity=0.8, color='lightblue')
    plotter.add_axes()
    
    print("\nShowing comparison...")
    print("Left: Raw mesh (similar to Gmsh)")
    print("Right: Surface extraction (what Python script shows)")
    plotter.show()

if __name__ == "__main__":
    print("=== Raw Mesh Visualization ===")
    print("\n1. Showing raw mesh (no processing)...")
    view_raw_mesh()
    
    print("\n2. Would you like to see a side-by-side comparison? (y/n)")
    # For now, just show the raw mesh
    # compare_views() 